package com.example.ivos.vehicle.service.impl;

import com.example.ivos.vehicle.mapper.VehicleMapper;
import com.example.ivos.vehicle.pojo.dto.VehicleParam;
import com.example.ivos.vehicle.pojo.dto.VehicleQuery;
import com.example.ivos.vehicle.pojo.entity.Vehicle;
import com.example.ivos.vehicle.pojo.vo.VehicleVO;
import com.example.ivos.vehicle.service.VehicleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class VehicleServiceImpl implements VehicleService {
    @Autowired
    private VehicleMapper vehicleMapper;

    @Override
    public List<VehicleVO> select(VehicleQuery vehicleQuery) {
        log.debug("业务层获取vehicleQuery:{}", vehicleQuery);
        List<VehicleVO> select = vehicleMapper.select(vehicleQuery);
        return select;
    }

    @Override
    public void save(VehicleParam vehicleParam) {
        log.debug("业务层获取vehicleVO:{}", vehicleParam);
        Vehicle vehicle= new Vehicle();
        BeanUtils.copyProperties(vehicleParam,vehicle);
        if(vehicle.getId()==null){
            vehicle.setStatus("1");
            vehicle.setGeofenceBindStatus("0");
            vehicle.setCreateTime(new Date());
            vehicleMapper.insert(vehicle);
        }
        else{
            vehicle.setUpdateTime(new Date());
            vehicleMapper.update(vehicle);
        }
    }

    @Override
    public void del(Long id) {
        log.debug("业务层:删除车辆id:{}", id);
        vehicleMapper.delVehicle(id);
    }

    @Override
    public void unbind(Long id) {
        log.debug("解绑车辆业务id:{}", id);
        Vehicle vehicle=new Vehicle();
        vehicle.setId(id);
        vehicle.setGeofenceBindStatus("0");
        vehicle.setUpdateTime(new Date());
        vehicle.setGeofenceId(null);
        vehicleMapper.unbind(vehicle);
    }

    @Override
    public void bind(VehicleParam vehicleParam) {
        log.debug("绑定车辆业务:{}", vehicleParam);
        Vehicle vehicle=new Vehicle();
        BeanUtils.copyProperties(vehicleParam,vehicle);
        vehicle.setGeofenceBindStatus("1");
        vehicle.setUpdateTime(new Date());
        vehicleMapper.update(vehicle);
    }
}
